*Table 14.3.5.1 Summary of Vital Signs (Clinical Safety Set);

%macro prnt (program=, idisname=, disname=, ds_list=, popt=, title=); 

proc freq data =adam.advs ;
   tables paramcd*param/ list missing nocum nopercent;
run;

%get_data(lst=advs,pre=adam,supp =N,lib=adam);
 
data tempor(keep=subjid usubjid param paramcd paramn aval avalc avisit avisitn VISITYP pd adt atm ADTM trtp trtpn ANL01FL);
  set adamadvs;
    if saffl="Y" and aval ne . ;
/*and  ANL01FL="Y";*/
/*and 	VISITYP="Scheduled";*/
/*if avisit=:"Scr" or avisit=:"Post" and VISITYP=:"Uns" then delete;*/
  param = tranwrd(tranwrd(param, "^9", "{\super\ 9}"), "^12", "{\super\ 12}");
  pd="T";
run;

*Obtain the visit where value closest to dosing time; 

proc sort data=tempor out=tempor;
by USUBJID PARAMN paramcd  AVISITN ADTM adt atm  ;
run;

data tempor; 
set tempor;   
by USUBJID  PARAMN paramcd  AVISITN ADTM adt atm ;
if avisitn <= 0 then do; if last.AVISITN; end;
else if avisitn > 0 then do; if first.AVISITN; end; 
run;


%macro pre(pr,m,par,period,parr);
proc sql;
create table &pr.n as select count(distinct usubjid) as N, &m as ord, avisit, avisitn, /*atpt,*/ "&par" as parameter length 24, "&parr" as unit length 50
	from tempor where paramcd="&par" group by &period, avisitn;
create table &pr.mean as select mean(aval) as mean format 6.0, &m as ord, avisit, avisitn, /*atpt,*/"&par" as parameter length 24, "&parr" as unit length 50 
	from tempor where paramcd="&par" group by &period, avisitn;
create table &pr.std as select std(aval) as sd format 6.1, &m as ord, avisit, avisitn, /*atpt,*/ "&par" as parameter length 24, "&parr" as unit length 50 
	from tempor where paramcd="&par" group by &period, avisitn;
create table &pr.med as select median(aval) as median format 6.1, &m as ord,  /*atpt,*/ avisit, avisitn, "&par" as parameter length 24, "&parr" as unit length 50 
	from tempor where paramcd="&par" group by &period, avisitn;
create table &pr.min as select min(aval) as min, &m as ord, avisit, avisitn,/*atpt,*/ "&par" as parameter length 24, "&parr" as unit length 50 
	from tempor where paramcd="&par" group by &period, avisitn;
create table &pr.max as select max(aval) as max, &m as ord, avisit, avisitn,/*atpt,*/ "&par" as parameter length 24, "&parr" as unit length 50 
	from tempor where paramcd="&par" group by &period, avisitn;
quit;

data &pr.1(drop=n);
set &pr.n;
length col col0 $20.;
col="n";
col0=strip(put(n,best.));
b=1;
run;

data &pr.2(drop=mean sd sdc sdc_);
merge &pr.mean &pr.std;
by &period /*atpt*/;
length col col0 $20.;
if sd ne . then do;
sdc=strip(put(sd,6.2));
sdc_=strip(put(sd,6.3));
end;
if sd=. then sdc="--";
if parameter ne "TEMP" then do;
col0=strip(put(mean,6.1))||" ("||strip(sdc)||")";
/*col0=strip(put(mean,6.0))||" ("||strip(put(sd,6.1))||")";*/
end;
if parameter eq "TEMP" then do;
col0=strip(put(mean,6.2))||" ("||strip(sdc_)||")";
end;
col="Mean (SD)";
b=2;
run;

data &pr.3(drop= median);
set &pr.med;
length col col0 $20.;
col="Median";
if parameter ne "TEMP" then do;
col0=strip(put(median,6.1));
end;
if parameter eq "TEMP" then do;
col0=strip(put(median,6.2));
end;
b=3;
run;

data &pr.4(drop=min max );
merge &pr.min &pr.max;
by &period /*atpt*/;
length col col0 $20.;
col="Min, Max";
if parameter ne "TEMP" then do;
col0=strip(put(min,best.))||", "||strip(put(max,best.));
end;
if parameter="TEMP" then do;
col0=strip(put(min,6.1))||", "||strip(put(max,6.1));
end;
b=4;
run;

data &pr;
set &pr.1 &pr.2 &pr.3 &pr.4;
run;

proc sort data=&pr; by avisitn &period ord; run;
%mend pre;
/*Heart Rate (beats/min)	PULSE*/

%pre(diabp,1,%str(DIABP),avisit,%str(Diastolic Blood Pressure (mmHg)));
%pre(hr,2,%str(PULSE),avisit,%str(Pulse Rate (beats/min)));
%pre(resp,3,%str(RESP),avisit,%str(Respiratory Rate (breaths/min)));
%pre(sysbp,4,%str(SYSBP),avisit,%str(Systolic Blood Pressure (mmHg)));
%pre(temp,5,%str(TEMP),avisit,%str(Temperature (C)));

data all; 
set diabp hr resp sysbp temp;
length dr $20.;
overall=col0;
/*if atpt="PREDOSE" then atptn=0;*/
/*else if atpt="3 HOURS" then atptn=3;*/
/*else atptn=.;*/
/*avisit=tranwrd(avisit,"Screening Day -28 to -1","Screening");*/
/*avisit=tranwrd(avisit,"Post Study Day 21/Early Termination","Post Study Day 21");*/
dr="Value";
run;

proc sort data=all; by ord avisitn /*atptn*/ b; run;

*Total number of patients by avisitn to display in a header;

proc sql noprint;
select count(distinct usubjid) into :n from adamadvs;
quit;

%let n=%sysfunc(compress(&n));
%put &n;


%page_cut(in=all,sort=ord unit avisitn avisit b,nocut=avisit,newpage=unit,pagesize=21,skip=unit,
	order=unit avisit col col0, columns=unit  avisit col col0, width=, defln=1,nlsym=\par,out=report);

*******************************************************************************************************
Printing using proc report
******************************************************************************************************;
title3 " ";
title4 "Table &disname (Continued)";
%footer1(foot1=1, ds_list=&ds_list.,program=&program.);

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&idisname..VS.SUM.rtf";

%open_rtf;
%empty_ds_fix(report);

title5 "Summary of Vital Signs";
title6 "(Safety Population)";

 
proc report data=report nowd split='~' missing spacing=1;
 column page ord unit parameter avisitn avisit /*atptn atpt*/ dr b col overall;

 define page       / " " order order=internal noprint;
 define ord     / " " order order=internal noprint;

 define unit / "Parameter (Unit)~ " order order=internal style(column)=[cellwidth=2.6in just=L] style(header)=[just=Left];
 define parameter / " " order order=internal noprint;

 define avisitn     / " " order order=internal noprint;
 define avisit / "Visit~ " order order=internal style(column)=[cellwidth=2.2in just=L] style(header)=[just=Left];

/* define atptn     / " " order order=internal noprint;*/
/* define atpt / " ~Time Point~ " order order=internal style(column)=[cellwidth=1.5in just=L] style(header)=[just=Left];*/
 define dr / " " order order=internal style(column)=[cellwidth=1.4in just=L] style(header)=[just=Left];

 define b     / " " order order=internal noprint;

 define col / " " order order=internal style(column)=[cellwidth=1.4in just=left] style(header)=[just=c];
 define overall / "Overall~ (N=&n)" order order=internal style(column)=[cellwidth=2.2in just=C] style(header)=[just=c];

break after page/page;
 
   compute before unit;
    line " ";
   endcomp;

   compute after avisit;
    line " ";
   endcomp;

run;
%close_rtf;
%arrange_rtf;
%mend prnt;

%prnt(program=T14_03_05_01_VS_SUM, idisname=14.3.5.1, disname=14.3.5.1, ds_list=ADVS);

%clrw;


